home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / ab20 / ab20_archive / utilities / shells / csh519s.lha / execom.c < prev    next >
C/C++ Source or Header  |  1992-02-26  |  39KB  |  1,542 lines

  1. /*
  2.  * EXECOM.C
  3.  *
  4.  * Matthew Dillon, 10 August 1986
  5.  * Version 2.07M by Steve Drew 10-Sep-87
  6.  * Version 4.01A by Carlo Borreo & Cesare Dieni 17-Feb-90
  7.  * Version 5.00L by Urban Mueller 17-Feb-91
  8.  *
  9.  */
  10.  
  11. #include "shell.h"
  12.  
  13. #define DEFAULTFRAME 1024
  14. #define Strlen(x)    strlen((char*)x)
  15. #define Strcpy(x,y)  strcpy((char*)x,(char*)y)
  16. #define Strcat(x,y)  strcat((char*)x,(char*)y)
  17. #define Index(x,y)   ((UBYTE*)index((char*)x,y))
  18. #define CHECKAV(num) { if( ac+num+10>max_ac ) checkav( frameptr, num ); }
  19.  
  20. typedef struct StackFrame {
  21.     struct StackFrame *next;
  22.     int  bytesleft;
  23.     char *ptr;
  24.     char mem[1];
  25. } FRAME;
  26.  
  27. /* execom.c */
  28. int   fcomm(UBYTE *str,FRAME **frame,char *from );
  29. int   hasspace( char *s );
  30. int   myfgets( char *buf, FILE *in );
  31.  
  32. static char  *compile_avf(FRAME **frame,char **av,int start,int end,char delim,int quote);
  33. static int   preformat(char *s, UBYTE *d);
  34. static void  backtrans(UBYTE *str);
  35. static UBYTE *exarg(UBYTE **ptr, UBYTE *elast);
  36. static UBYTE *format_insert_string(FRAME **frameptr, UBYTE *str, char *from);
  37. static int   find_command(char *str);
  38. static int   checkav( FRAME **frame,int n );
  39. static char  *tempname( int which );
  40. static UBYTE *skipword( UBYTE *strskip );
  41.  
  42. static void  newframe( FRAME **frame, int bytes);
  43. static void  *falloc( FRAME **frame, int bytes );
  44. static void  *frealloc( FRAME **frame, char *oldstring, int morebytes );
  45. static void  funalloc( FRAME **frame, int bytes );
  46. static char  *push_cpy( FRAME **frame, void *s);
  47. static void  deleteframe( FRAME *frame);
  48.  
  49. void  exec_every(void);
  50.  
  51. int do_nothing(void);
  52.  
  53. struct COMMAND {
  54.     int (*func)();
  55.     short minargs;
  56.     short stat;
  57.     int val;
  58.     char *name;
  59.     char *options;
  60.     char *usage;
  61. };
  62.  
  63. #define ST_COND   0x01 /* this is an 'if' type command, no redirection */
  64. #define ST_NORED  0x02 /* this command can't be redirected             */
  65. #define ST_NOEXP  0x04 /* no wild card expansion for this command      */
  66. #define ST_A0     0x08 /* delimit args with 0xA0 instead of ' '        */
  67. #define ST_FUNC   0x10 /* can be used as a function                    */
  68. #define ST_STORED 0x20 /* stores redirection instead of execuing it    */
  69. #define ST_AVLINE 0x40 /* needs avline                                 */
  70.  
  71. #define COND  ST_COND
  72. #define NORED ST_NORED
  73. #define STRED ST_STORED
  74. #define NOEXP ST_NOEXP
  75. #define A0   (ST_A0|ST_AVLINE)
  76. #define AV    ST_AVLINE
  77. #define FUNC  ST_FUNC
  78.  
  79. #define ALIAS LEVEL_ALIAS
  80. #define SET   LEVEL_SET
  81.  
  82. #define FIRSTCOMMAND 4
  83. #define COM_EXEC 3
  84.  
  85. static struct COMMAND Command[] = {
  86.  do_run,       0,   A0,     0, "\001",       NULL, NULL, /* may call do_source, do_cd */
  87.  do_nothing,   0,    0,     0, "\002",       NULL, NULL, /* dummy for aliases   */
  88.  do_nothing,   0,    0,     0, "\003",       NULL, NULL, /* dummy for aliases with args */
  89.  do_run,       0,NORED|A0,  1, "\004",       NULL, NULL, /* external that needs to be Execute()d */
  90.  do_abortline, 0,    0,     0, "abortline",  NULL, "",
  91.  do_action,    2,   AV,     9, "action",     "av", "action file [args]",
  92.  do_addbuffers,2,    0,     0, "addbuffers", NULL, "{drive bufs}",
  93.  do_set_var,   0,    0, ALIAS, "alias",      NULL, "[name [string] ]",
  94.  do_ascii,     0,    0,     0, "ascii",      "oh", "-oh [string]",
  95.  do_aset,      1,    0,     0, "aset",       NULL, "name value",
  96.  do_assign,    0,    0,     0, "assign",     "lnp",",logical,-lnp {logical physical}",
  97.  do_basename,  2, FUNC,     0, "basename",   NULL, "var path",
  98.  do_cat,       0,    0,     0, "cat",        "n",  "-n [file file...]",
  99.  do_cd,        0,    0,     0, "cd",         "g",  "[path],-g path...path",
  100.  do_class,     0,   A0,     0, "class",      "n",  "-n name {type=param} \"actions\" {action=command}",
  101.  do_close,     0,    0,     0, "close",      NULL, "filenumber",
  102.  do_copy,      1,    0,     0, "copy",       "rudpfmqo","-dfmopqru file file,-ud file...file dir,-ud dir...dir dir",
  103.  do_copy,      1,    0,     0, "cp",         "rudpfmqo","-dfmopqru file file,-ud file...file dir,-ud dir...dir dir",
  104.  do_date,      0,    0,     0, "date",       "sr", "-sr [date/time]",
  105.  do_inc,       1,    0,    -1, "dec",        NULL, "varname [step]",
  106.  do_rm,        0,    0,     0, "delete",     "rpq","-pqr file...file",
  107.  do_dir,       0,NOEXP,     0, "dir",        "sfdcnhltbuikqavopzeg","-abcdefghiklnopqstuv [-z lformat] [path...path]",
  108.  do_diskchange,1,    0,     0, "diskchange", NULL, "drive",
  109.  do_echo,      0,    0,     0, "echo",       "ne", "-ne string",
  110.  do_if,        0, COND,     1, "else",       NULL, "",
  111.  do_if,        0, COND,     2, "endif",      NULL, "",
  112.  do_error,     1,    0,     0, "error",      NULL, "num",
  113.  do_exec,      1,   AV,     0, "exec",       "i",  "-i command",
  114.  do_fault,     1,    0,     0, "fault",      NULL, "error",
  115.  do_filenote,  1,    0,     0, "filenote",   "s",  "file...file note,-s file...file",
  116.  do_fileslist, 0,    0,     0, "flist",      NULL, "",
  117.  do_fltlower,  0,    0,     0, "fltlower",   NULL, "<in >out",
  118.  do_fltupper,  0,    0,     0, "fltupper",   NULL, "<in >out",
  119.  do_foreach,   3,    0,     0, "foreach",    "v",  "-v varname ( string ) command",
  120.  do_forever,   1,   AV,     0, "forever",    NULL, "command",
  121.  do_forline,   3,    0,     0, "forline",    NULL, "var filename command",
  122.  do_fornum,    4,    0,     0, "fornum",     "vs", "-vs var n1 n2 command",
  123.  do_getenv,    1, FUNC,     0, "getenv",     NULL, "shellvar envvar",
  124.  do_goto,      1,    0,     0, "goto",       NULL, "label",
  125.  do_head,      1,    0,     0, "head",       NULL, "filename [num]",
  126.  do_help,      0,    0,     0, "help",       NULL, "",
  127.  do_history,   0,    0,     0, "history",    "nr", "-nr [partial_string]",
  128.  do_howmany,   0,    0,     0, "howmany",    NULL, "",
  129.  do_htype,     1,    0,     0, "htype",      "r",  "-r file...file",
  130.  do_if,        1,COND|NORED,0, "if",         "rftmdvone","-n arg cond arg,-n arg,-nf file,-nd dir,-nm,-nt file...file,-nr rpn_expr,-no opt args,-v varname",
  131.  do_inc,       1,    0,     1, "inc",        NULL, "varname [step]",
  132.  do_info,      0,    0,     0, "info",       NULL, "[drive...drive]",
  133.  do_input,     1,    0,     0, "input",      "sr", "-rs var...var",
  134.  do_join,      2,    0,     1, "join",       "r",  "-r file...file",
  135.  do_keymap,    1,    0,     0, "keymap",     "n",  "-n number {key=function}",
  136.  do_label,     1, COND,     0, "label",      NULL, "name",
  137.  do_local,     0,    0,     0, "local",      NULL, "[var...var]",
  138.  do_linecnt,   0,    0,     0, "linecnt",    NULL, "<in >out",
  139.  do_dir,       0,NOEXP,     0, "ls",         "sfdcnhltbuikqavopzeg","-abcdefghiklnopqstuv [-z format] [path...path]",
  140.  do_man,       0,    0,     0, "man",        NULL, "command...command",
  141.  do_mkdir,     0,    0,     0, "md",         NULL, "name...name",
  142.  do_mem,       0,    0,     0, "mem",        "cfqsrl","-cflqsr",
  143.  do_menu,      0,    0,     0, "menu",       "n",  "-n [title item...item]",
  144.  do_mkdir,     0,    0,     0, "mkdir",      NULL, "name...name",
  145.  do_mv,        2,    0,     0, "mv",         NULL, "from to,from...from todir",
  146.  do_open,      3,    0,     0, "open",       NULL, "file mode number",
  147.  do_path,      0,    0,     0, "path",       "r",  "-r [dir...dir]",
  148.  do_pri,       2,    0,     0, "pri",        NULL, "clinumber pri,0 pri",
  149.  do_protect,   2,    0,     0, "protect",    NULL, "file...file flags",
  150.  do_ps,        0,    0,     0, "ps",         "le", "-el [commandname...commandname]",
  151.  do_pwd,       0,    0,     0, "pwd",        NULL, "",
  152.  do_qsort,     0,    0,     0, "qsort",      "r",  "-r <in >out",
  153.  do_quit,      0,NORED,     0, "quit",       NULL, "",
  154.  do_truerun,   1,NORED|AV,  1, "rback",      NULL, "command",
  155.  do_mv,        2,    0,     0, "rename",     NULL, "from to,from...from todir",
  156.  do_readfile,  1,    0,     0, "readfile",   NULL, "varname [filename]",
  157.  do_relabel,   2,    0,     0, "relabel",    NULL, "drive name",
  158.  do_resident,  0,    0,     0, "resident",   "ard",",-ard file...file",
  159.  do_return,    0,    0,     0, "return",     NULL, "[n]",
  160.  do_rm,        0,    0,     0, "rm",         "rpq","-pqr file...file",
  161.  do_rpn,       0,NOEXP,     0, "rpn",        NULL, "expression",
  162.  do_rxrec,     0,    0,     0, "rxrec",      NULL, "[portname]",
  163.  do_rxsend,    2,   AV,     0, "rxsend",     "rl", "-lc portname string",
  164.  do_truerun,   1,STRED|AV,  0, "run",        NULL, "command",
  165.  do_search,    2,    0,     0, "search",     "rcwneqvbfalo","-abceflnoqrvw file...file string",
  166.  do_set_var,   0,    0,   SET, "set",        NULL, "[name [string] ]",
  167.  do_setenv,    2,    0,     0, "setenv",     NULL, "var value",
  168.  do_sleep,     0,    0,     0, "sleep",      NULL, "timeout",
  169.  do_split,     1,    0,     0, "split",      NULL, "srcvar dstvar...dstvar",
  170.  do_source,    1,   A0,     0, "source",     NULL, "file",
  171.  do_stack,     0,    0,     0, "stack",      NULL, "[bytes]",
  172.  do_strhead,   3, FUNC,     0, "strhead",    NULL, "varname breakchar string",
  173.  do_strings,   1,    0,     0, "strings",    "r",  "-r file...file minlength",
  174.  do_strleft,   3, FUNC,     0, "strleft",    NULL, "varname string n",
  175.  do_strlen,    2, FUNC,     0, "strlen",     NULL, "varname string",
  176.  do_strmid,    3, FUNC,     0, "strmid",     NULL, "varname string n1 [n2]",
  177.  do_strright,  3, FUNC,     0, "strright",   NULL, "varname string n",
  178.  do_strtail,   3, FUNC,     0, "strtail",    NULL, "varname breakchar string",
  179.  do_tackon,    3, FUNC,     0, "tackon",     NULL, "var pathname filename",
  180.  do_head,      1,    0,     1, "tail",       NULL, "filename [num]",
  181.  do_tee,       0,    0,     0, "tee",        NULL, "<in >out",
  182.  do_touch,     0,    0,     0, "touch",      NULL, "file...file",
  183.  do_truncate,  0,    0,     0, "truncate",   NULL, "<in >out",
  184.  do_cat,       0,    0,     0, "type",       "n",  "-n [file...file]",
  185.  do_unset_var, 0,   AV, ALIAS, "unalias",    NULL, "name...name",
  186.  do_uniq,      0,    0,     0, "uniq",       NULL, "<in >out",
  187.  do_unset_var, 0,    0,   SET, "unset",      NULL, "name...name",
  188.  do_usage,     0,    0,     0, "usage",      NULL, "[command...command]",
  189.  do_ver,       0,    0,     0, "version",    NULL, "",
  190.  do_waitport,  1,    0,     0, "waitforport",NULL, "portname [seconds]",
  191.  do_whereis,   1,NOEXP,     0, "whereis",    "r",  "-r file [path...path]",
  192.  do_window,    0,NOEXP,     0, "window",     "slfbaq","-slfbaq",
  193.  do_writefile, 1,    0,     0, "writefile",  NULL, "var >out",
  194.  NULL,         0,    0,     0, NULL,         NULL, NULL,
  195. };
  196.  
  197.  
  198. /* do_which,     1,    0,     0, "which",      NULL, "command", */
  199.  
  200. #ifdef isalphanum
  201. char isalph[256];
  202. #endif
  203.  
  204. int
  205. execute( char *str )
  206. {
  207.     ULONG toptions=options;
  208.     int   ret, oldac=ac, oldmax=max_ac;
  209.     char  **oldav=av;
  210.  
  211.     if( !str ) return -1;
  212.  
  213.     ++H_stack;
  214.     ret=exec_command(str);
  215.     --H_stack;
  216.  
  217.     av=oldav; max_ac=oldmax; ac=oldac;
  218.     options=toptions;
  219.  
  220.     return ret;
  221. }
  222.  
  223. int
  224. exec_command( char *base )
  225. {
  226.     FRAME *frame=NULL;
  227.     UBYTE *scr;
  228.     char  buf[6];
  229.     int   len, newlen, ret;
  230.  
  231.     if (!H_stack && S_histlen>1) {
  232.         add_history(base);
  233.         sprintf(buf, "%d", H_tail_base + H_len);
  234.         set_var(LEVEL_SET, v_histnum, buf);
  235.     }
  236.  
  237.     len=Strlen(base)*3+20;
  238.     newframe( &frame, len+DEFAULTFRAME );
  239.  
  240.     scr   = falloc(&frame,len);
  241.     newlen= preformat( base,scr );
  242.  
  243.     funalloc( &frame, len-newlen-2 );
  244.     ret=fcomm(scr,&frame,NULL);
  245.  
  246.     deleteframe( frame );
  247.  
  248.     return ret;
  249. }
  250.  
  251. #ifndef isalphanum
  252. isalphanum( char c )
  253. {
  254.     return (
  255.         (c >= 'a' && c <= 'z') ||
  256.         (c >= 'A' && c <= 'Z') ||
  257.         (c >= '0' && c <= '9') ||
  258.         (c == '_')
  259.     );
  260. }
  261. #endif
  262.  
  263. #define HOT_GAP    0x80
  264. #define HOT_BLANK  0x81
  265. #define HOT_STAR   0x82
  266. #define HOT_QUES   0x83
  267. #define HOT_EXCL   0x84
  268. #define HOT_SEMI   0x85
  269. #define HOT_PIPE   0x86
  270. #define HOT_DOLLAR 0x87
  271. #define HOT_IN     0x88
  272. #define HOT_OUT    0x89
  273. #define HOT_BSLASH 0x8a
  274. #define HOT_LASTCD 0x8b
  275. #define HOT_BACKG  0x8c
  276. #define HOT_CURLL  0x8d
  277. #define HOT_CURLR  0x8e
  278. #define HOT_CURDIR 0x8f
  279. #define HOT_PARDIR 0x90
  280. #define HOT_BEGOUT 0x91
  281. #define HOT_ENDOUT 0x92
  282. #define HOT_EQUAL  0x93
  283. #define HOT_BEGIN  0x94
  284. #define HOT_APOSTR 0x95
  285.  
  286. /* USHORT ArgPos[256]; */
  287.  
  288. static UBYTE termchar[]={ 0,    '}',       ')',        '`' };
  289. static UBYTE hotchar []={ 0, HOT_CURLR, HOT_ENDOUT, HOT_APOSTR };
  290.  
  291. static int
  292. preformat( char *src, UBYTE *d )
  293. {
  294.     int qm=0, i, level, curmode, argc=0, beg=1;
  295.     UBYTE mode[100], c, *s=(UBYTE*)src, *start=d;
  296.  
  297.     while (*s) {
  298.         if (qm ) {
  299.             while( isalphanum( *s ) || (*s && *s!='\"' && *s!='\\' && *s!='\n'))
  300.                 *d++ = *s++;
  301.             if( !*s ) break;
  302.             if( *s=='\n' ) qm=0, *s=';';
  303.         }
  304.         if( beg ) c=HOT_BEGIN,beg=0; else c=*s;
  305.         switch (c) {
  306.         case ' ':
  307.         case 9:
  308.             *d++ = HOT_BLANK;
  309. argstart:
  310.             while (*s==' ' || *s==9) ++s;
  311.             if( argc && (!*s || *s=='|' || *s==';' || *s=='#' || *s=='\n' ))--d;
  312.             if(      *s=='\\' && !argc ) { *d++=HOT_BSLASH; if( *d++=*++s ) ++s; }
  313.             else if( *s=='~'  )          { *d++=HOT_LASTCD; s++; }
  314.             else if( *s=='.'  )
  315.                 for( ;; ) {
  316.                     if( s[0]=='.' && issep(s[1]))
  317.                         s+=1+(s[1]=='/');
  318.                     else if( s[0]=='.' && s[1]=='.' && issep(s[2]))
  319.                         *d++='/', s+=2+(s[2]=='/');
  320.                     else 
  321.                         break;
  322.                 }
  323.             argc++;
  324.             break;
  325.         case '*':
  326.             *d++ = HOT_GAP;
  327.             *d++ = HOT_STAR;
  328.             ++s;
  329.             break;
  330.         case '?':
  331.             *d++ = HOT_GAP;
  332.             *d++ = HOT_QUES;
  333.             ++s;
  334.             break;
  335.         case '!':
  336.             *d++ = HOT_EXCL;
  337.             ++s;
  338.             break;
  339.         case HOT_BEGIN:
  340.             argc=0;
  341.             goto argstart;
  342.         case '#':
  343.             while (*s && *s!='\n') ++s;
  344.             if( !*s ) break;
  345.         case '\n':
  346.         case ';':
  347.             *d++= HOT_SEMI, argc=0, s++;
  348.             goto argstart;
  349.         case '|':
  350.             *d++= HOT_PIPE, argc=0, s++;
  351.             goto argstart;
  352.         case '\\':
  353.             if( (i=*++s-'0')>=0 && i<=7 ) {
  354.                 if( *++s>='0' && *s<='7' ) {
  355.                     i= 8*i + *s++-'0';
  356.                     if( *s>='0' && *s<='7' )
  357.                         i= 8*i + *s++-'0';
  358.                 }
  359.                 *d++ = i;
  360.             } else {
  361.                 *d++ = *s;
  362.                 if (*s) ++s;
  363.             }
  364.             break;
  365.         case '\"':
  366.             qm = 1 - qm;
  367.             ++s;
  368.             break;
  369.         case '^':
  370.             *d++ = *++s & 0x1F;
  371.             if (*s) ++s;
  372.             break;
  373.         case '<':
  374.             *d++ = HOT_IN;
  375.             ++s;
  376.             break;
  377.         case '>':
  378.             *d++ = HOT_OUT;
  379.             ++s;
  380.             break;
  381.         case '&':
  382.             *d++ = HOT_BACKG;
  383.             ++s;
  384.             break;
  385.         case '$': /* search end of var name */
  386.             if( s[1]=='(' ) {
  387.                 curmode=2;
  388.                 *d++=HOT_BEGOUT;
  389.                 *d++=*++s;
  390.                 goto brace;
  391.             }
  392.             *d++ = HOT_GAP;
  393.             *d++ = HOT_DOLLAR;
  394.             ++s;
  395.             while (isalphanum(*s)) *d++ = *s++;
  396.             *d++ = HOT_GAP;
  397.             break;
  398.         case '`':
  399.             curmode=3;
  400.             *d++=HOT_APOSTR;
  401.             goto brace;
  402.         case '{':
  403.             curmode=1;
  404.             *d++ = HOT_CURLL;
  405. brace:
  406.             level=0; s++;
  407.             mode[level++]=curmode;
  408.             while( *s && level ) {
  409.                 switch( *s ) {
  410.                 case '\"': *d++=*s++;
  411.                            while( *s && *s!='\"' && *s!='\n')
  412.                                if( *s=='\\' ) { *d++=*s++; if( *s ) *d++=*s++; }
  413.                                else *d++=*s++;
  414.                            if( *s ) *d++=*s++;
  415.                            break;
  416.                 case '\\': *d++=*s++; if( *s ) *d++=*s++;
  417.                            break;
  418.                 case '{' : mode[level++]=1; *d++=*s++;
  419.                            break;
  420.                 case '}' :
  421.                 case ')' :
  422.                 case '`' : for( i=level-1; i>=0 && termchar[mode[i]]!=*s;--i ) ;
  423.                            if( i==0 ) *d++=hotchar [mode[i]];
  424.                            else       *d++=*s;
  425.                            if( i>=0 ) level=i;
  426.                            s++;
  427.                            break;
  428.                 default  : *d++=*s++;
  429.                            break;
  430.                 }
  431.             }
  432.             break;
  433.         default:
  434.             *d++ = *s++;
  435.             while( *s>=65 && (*s&31)<=26 )
  436.                 *d++=*s++;
  437.             break;
  438.         }
  439.     }
  440. endwhile:
  441.     *d++=0;
  442.     *d=0;
  443.     if (debug) fprintf (stderr,"PREFORMAT: %d :%s:\n", Strlen(start), start);
  444.     return d-start;
  445. }
  446.  
  447. static void
  448. backtrans( UBYTE *str )
  449. {
  450.     if( !str )
  451.         return;
  452.  
  453.     while( *str ) {
  454.         while( *(signed char *)str>0 ) str++;
  455.         if( !*str ) break;
  456.         switch( *str) {
  457.             case HOT_GAP   : *str++=0;    break;
  458.             case HOT_BLANK : *str++=' ';  break;
  459.             case HOT_STAR  : *str++='*';  break;
  460.             case HOT_QUES  : *str++='?';  break;
  461.             case HOT_EXCL  : *str++='!';  break;
  462.             case HOT_SEMI  : *str++=';';  break;
  463.             case HOT_PIPE  : *str++='|';  break;
  464.             case HOT_DOLLAR: *str++='$';  break;
  465.             case HOT_IN    : *str++='<';  break;
  466.             case HOT_OUT   : *str++='>';  break;
  467.             case HOT_BSLASH: *str++='\\'; break;
  468.             case HOT_LASTCD: *str++='~';  break;
  469.             case HOT_BACKG : *str++='&';  break;
  470.             case HOT_CURLL : *str++='{';  break;
  471.             case HOT_CURLR : *str++='}';  break;
  472.             case HOT_CURDIR: *str++='.';  break;
  473.             case HOT_PARDIR: *str++='.';  break;
  474.             case HOT_BEGOUT: *str++='$';  break;
  475.             case HOT_ENDOUT: *str++=')';  break;
  476.             case HOT_EQUAL : *str++='=';  break;
  477.             case HOT_APOSTR: *str++='`';  break;
  478.             default        : str++;       break;
  479.         }
  480.     }
  481. }
  482.  
  483. /*
  484.  * process formatted string.  ' ' is the delimeter.
  485.  *
  486.  *    0: check '\0': no more, stop, done.
  487.  *    1: check $.     if so, extract, format, insert
  488.  *    2: check alias. if so, extract, format, insert. goto 1
  489.  *    3: check history or substitution, extract, format, insert. goto 1
  490.  *
  491.  *    4: assume first element now internal or disk based command.
  492.  *
  493.  *    5: extract each ' ' or 0x80 delimited argument and process, placing
  494.  *       in av[] list (except 0x80 args appended).  check in order:
  495.  *
  496.  *             '$'         insert string straight
  497.  *             '>'         setup stdout
  498.  *             '>>'        setup stdout flag for append
  499.  *             '<'         setup stdin
  500.  *             '*' or '?'  do directory search and insert as separate args.
  501.  *
  502.  *             ';' 0 '|'   end of command.  if '|' setup stdout
  503.  *                          -execute command, fix stdin and out (|) sets
  504.  *                           up stdin for next guy.
  505.  */
  506.  
  507. int  alias_count;
  508. int  has_wild;                 /* set if any arg has wild card */
  509. char *LastCommand;
  510. BPTR redir_out, redir_in;
  511.  
  512. #define MAXACDEF 32
  513.  
  514. int
  515. fcomm( UBYTE *str, FRAME **frameptr, char *from )
  516. {
  517.     UBYTE *nextstr, elast;
  518.     char *pend_alias, *command;
  519.     char cout_ispipe=0, cin_ispipe=0, cout_append=0;
  520.     char backg, firstrun, override, block, nobuiltin;
  521.     char *cin_name=NULL, *cout_name=NULL;
  522.     char **oldav=av;
  523.     signed char pendredir;
  524.     int  oldac=ac, oldmax=max_ac, err, ccno, cstat;
  525.  
  526.     max_ac= MAXACDEF;
  527.     av=(char **)falloc( frameptr, max_ac*sizeof(char *));
  528.     ac=0;
  529.  
  530.     if (++alias_count >= MAXALIAS) {           /* Recursion getting too deep? */
  531.         fprintf(stderr,"Alias Loop\n");
  532.         err = 20;
  533.         goto done1;
  534.     }
  535.  
  536. nextcommand:
  537.     command   = NULL;
  538.     pend_alias= NULL;
  539.     err       = 0;
  540.     has_wild  = 0;
  541.     firstrun  = 1;
  542.     ccno=cstat= 0;
  543.     elast     = 1;
  544.     block     = 0;
  545.     nobuiltin = 0;
  546.     pendredir = 0;
  547.     redir_in  = redir_out = 0;
  548.  
  549.     if (*str == 0)
  550.         goto done1;
  551.  
  552.     if ( *str == HOT_EXCL) {
  553.         UBYTE *p, c;
  554.         char *istr;                       /* fix to allow !cmd1;!cmd2 */
  555.         for(p = str; *p && *p != HOT_SEMI ; ++p);
  556.         c = *p;
  557.         *p = 0;
  558.         if( str[1]==HOT_EXCL ) str[1]='!';
  559.         istr = get_history((char *)str);
  560.         *p = c;
  561.         replace_head(istr);
  562.         str = format_insert_string( frameptr, str, istr );
  563.     }
  564.  
  565.     /*******************************************************
  566.      * Part one of the parser:
  567.      * The argument line is generated as an array of strings
  568.      */
  569.  
  570.     nextstr = str;
  571.     ac = 0;
  572.     do {              /* outer loop: each pass typically generates one av[ac] */
  573.         UBYTE *ptr, *arg;
  574.         signed char redir, doexpand, cont, inc;
  575.  
  576.         av[ac] = NULL;
  577.         cont = 1;
  578.         doexpand = redir = inc = 0;
  579.  
  580.         while (cont && elast) {       /* inner loop: adds one piece to av[ac] */
  581.             ptr = exarg(&nextstr,&elast);
  582.             inc = 1;
  583.             cont = (elast == 0x80);
  584.             if( (UBYTE)*ptr<0x80 )
  585.                 arg=ptr;
  586.             else {
  587.                 switch ((UBYTE)*ptr) {           /* arg must be set in every case */
  588.                 case HOT_IN:
  589.                     redir = -2;
  590.                 case HOT_OUT:
  591.                     if (cstat & (ST_NORED | ST_COND)) {     /* don't extract   */
  592.                         redir = 0;                          /* <> stuff if its */
  593.                         arg = ptr;                          /* external cmd.   */
  594.                         break;
  595.                     }
  596.                     ++redir;
  597.                     arg = ptr + 1;
  598.                     if (*arg == HOT_OUT) {
  599.                         redir = 2;        /* append >> */
  600.                         ++arg;
  601.                     }
  602.                     cont = 1;
  603.                     break;
  604.                 case HOT_DOLLAR:
  605.                     /* restore args if from set command or pend_alias */
  606.                     if ((arg = get_var(LEVEL_SET, ptr + 1))) {
  607.                         UBYTE *pe, sv;
  608.                         while (pe = Index(arg,0xA0)) {
  609.                             sv = *pe;
  610.                             *pe = '\0';
  611.                             CHECKAV( 1 );
  612.  
  613.                             if (av[ac]) {
  614.                                 av[ac] = frealloc( frameptr,av[ac],Strlen(arg));
  615.                                 Strcat(av[ac++], arg);
  616.                             } else
  617.                                 av[ac++] = push_cpy(frameptr,arg);
  618.  
  619.                             *pe = sv;
  620.                             av[ac] = NULL;
  621.                             arg = pe+1;
  622.                         }
  623.                     } else
  624.                         arg = ptr;
  625.                     break;
  626.                 case HOT_LASTCD:
  627.                     if ((!ptr[1] || ptr[1]=='/')&&(arg=get_var(LEVEL_SET, v_lcd))) {
  628.                         if( ptr[1] ) {
  629.                             Strcpy(Buf,arg);
  630.                             Strcat(Buf,ptr+1);
  631.                             arg=(UBYTE*)Buf;
  632.                         } 
  633.                     } else
  634.                         arg = ptr;
  635.                     break;
  636.                 case HOT_CURDIR:
  637.                     arg=ptr;
  638.                     if( !ptr[1] && elast!=0x80 || ptr[1]=='/' ) {
  639.                         if( ac==0 ) nobuiltin=1;
  640.                         arg= ptr[1]=='/' ? ptr+2 : ptr+1;
  641.                     }
  642.                     break;
  643.                 case HOT_PARDIR:
  644.                     arg=ptr;
  645.                     if( !ptr[2] && elast!=0x80 || ptr[2]=='/' )
  646.                         arg= ptr[2]=='/' ? ptr+2 : (UBYTE*)"/";
  647.                     break;
  648.  
  649.                 case HOT_STAR:
  650.                 case HOT_QUES:
  651.                     if( !(cstat & ST_NOEXP) && !(pend_alias && *pend_alias=='*'))
  652.                         if( ac!=1 || av[1]&&*av[1] || *ptr!=HOT_QUES || ptr[1] )
  653.                             doexpand = 1;
  654.                     arg = ptr;
  655.                     break;
  656.                 default:
  657.                     arg = ptr;
  658.                     break;
  659.                 }
  660.             }
  661.  
  662.             /* Append arg to av[ac] */
  663.  
  664.             if (av[ac]) {
  665.                 av[ac] = frealloc( frameptr,av[ac],Strlen(arg));
  666.                 Strcat(av[ac], arg);
  667.             } else {
  668.                 av[ac] = push_cpy( frameptr, arg );
  669.             }
  670.  
  671.             if (elast != 0x80)
  672.                 break;
  673.         }
  674.  
  675.         /* one argument is now complete */
  676.  
  677.         backg   = *av[ac] && (UBYTE)av[ac][Strlen(av[ac])-1]==HOT_BACKG;
  678.         override= (UBYTE)*av[ac] == HOT_BSLASH;
  679.  
  680.         if( firstrun && (UBYTE)*av[0] == HOT_CURLL ) {
  681.             char *end;
  682.             av[0]++;
  683.             if( end=index(av[0],HOT_CURLR)) *end=0;
  684.             block=1;
  685.         }
  686.  
  687.         if( (UBYTE)*av[ac]==HOT_BEGOUT || (UBYTE)*av[ac]==HOT_APOSTR ) {
  688.             BPTR cout, oldcout= Myprocess->pr_COS;
  689.             FILE *in;
  690.             UBYTE *t;
  691.             int  apo;
  692.  
  693.             apo= (UBYTE)*av[ac]==HOT_APOSTR;
  694.             inc=0;
  695.             if( t=Index(av[ac]+1, apo ? HOT_APOSTR : HOT_ENDOUT ))
  696.                 *t=0;
  697.  
  698.             if(!(cout = Open(tempname(2),MODE_NEWFILE))) {
  699.                 err= 20;
  700.                 ierror (NULL, 504);
  701.             } else {
  702.                 Myprocess->pr_COS = DEVTAB(stdout) = cout;
  703.                 execute(av[ac]+2-apo);
  704.                 fflush(stdout);
  705.                 Close(cout);
  706.  
  707.                 if(!(in=fopen(tempname(2),"r"))) {
  708.                     err= 1;
  709.                     ierror (NULL, 504);
  710.                 } else {
  711.                     while( myfgets(Buf,in)) {
  712.                         char *str=Buf, *get, quote=0;
  713.                         CHECKAV( 1 );
  714.                         if( apo ) {
  715.                             while( *str==' ' ) str++;
  716.                             for( get=str; *get; ) {
  717.                                 if( *get=='\"' )
  718.                                     quote=1-quote, get++;
  719.                                 else if( *get==' ' && !quote ) {
  720.                                     *get++=0;
  721.                                     CHECKAV( 1 );
  722.                                     while( *get==' ' ) get++;
  723.                                     if( !*get ) break;
  724.                                     av[ac++]=push_cpy(frameptr,str);
  725.                                     str=get;
  726.                                 } else 
  727.                                     get++;
  728.                             }
  729.                             av[ac++]= push_cpy(frameptr,str);
  730.                         } else 
  731.                             av[ac++]= push_cpy(frameptr,(UBYTE*)Buf);
  732.                     }
  733.                     fclose(in);
  734.                     DeleteFile(tempname(2));
  735.                     av[ac]=NULL;
  736.                 }
  737.             }
  738.  
  739.             Myprocess->pr_COS = DEVTAB(stdout) = oldcout;
  740.         }
  741.  
  742.         backtrans( (UBYTE *)av[ac] );
  743.  
  744.         if (doexpand) {                                  /* process expansion */
  745.             char **eav, **ebase;
  746.             int eac;
  747.             has_wild = 1;
  748.             eav = ebase = expand(av[ac], &eac);
  749.             inc = 0;
  750.             if (eav) {
  751.                 /* FUTURE: check retval */
  752.                 CHECKAV( eac );
  753.                 for (; eac; --eac, ++eav)
  754.                     av[ac++] = push_cpy(frameptr,*eav);
  755.                 free_expand (ebase);
  756.             }
  757.         } else if( av[ac] && av[ac][0]==')' ) {
  758.             int i;
  759.             UBYTE *pe, sv;
  760.             for( i=ac-1; i>0; i-- )
  761.                 if( *av[i]=='@' )
  762.                     break;
  763.             if( i>0 && av[i][Strlen(av[i])-1]=='(' ) {
  764.                 extern int exec_fn_err;
  765.                 char *avi=av[i], *last=av[ac];
  766.                 av[i]=v_value; av[ac]=NULL;
  767.                 arg=(UBYTE*)exec_function( avi+1, av+i, ac-i );
  768.                 av[i]=avi;     av[ac]=last;
  769.                 inc=0;
  770.                 if( exec_fn_err<0 )
  771.                     ac++;
  772.                 else if( exec_fn_err>0 || !arg )
  773.                     ac=i, av[ac]=NULL;
  774.                 else {
  775.                     ac=i;
  776.                     while (pe = Index(arg,0xA0)) {
  777.                         sv = *pe;
  778.                         *pe = '\0';
  779.                         CHECKAV( 1 );
  780.                         av[ac++] = push_cpy(frameptr,arg);
  781.                         *pe = sv;
  782.                         arg= pe+1;
  783.                     }
  784.                     av[ac] = falloc(frameptr,Strlen(arg)+Strlen(last+1)+4);
  785.                     Strcpy(av[ac],arg);
  786.                     Strcat(av[ac++], last+1 );
  787.                 }
  788.             }
  789.         }
  790.  
  791.         /*******************************
  792.          * special handling of first arg
  793.          */
  794.  
  795.         if( firstrun ) {                     /* we just found out the command */
  796.             firstrun=0;
  797.             command=av[0];
  798.  
  799.             if (*command == 0)
  800.                 goto done0;
  801.  
  802.             if( block )
  803.                 pend_alias=command;
  804.             else if ( override )
  805.                 memmove( command, command+1, Strlen(command));
  806.             else {
  807.                 pend_alias=get_var(LEVEL_ALIAS,command);  /* if not \command */
  808.                 if( pend_alias && pend_alias==from )
  809.                     pend_alias=NULL;
  810.             }
  811.  
  812.             if( pend_alias )
  813.                 ccno= *pend_alias=='%' || *pend_alias=='*' ? 2 : 1;
  814.             else 
  815.                 ccno= nobuiltin ? 0 : find_command(command);
  816.             cstat=Command[ccno].stat;
  817.  
  818.             if ( !(cstat & ST_COND) && (disable || forward_goto) ) {
  819.                 while (elast && elast != HOT_SEMI && elast != HOT_PIPE)
  820.                     exarg(&nextstr,&elast);
  821.                 goto done0;
  822.             }
  823.         }
  824.  
  825.         /********************************
  826.          * handling of redirection
  827.          */
  828.  
  829.         if( pendredir )
  830.             redir=pendredir, pendredir=0;
  831.  
  832.         if (redir && !err) {                            /* store redirection  */
  833.             char *file = (doexpand) ? av[--ac] : av[ac];
  834.  
  835.             if( !*file )
  836.                 pendredir=redir;
  837.             else if (redir < 0)
  838.                 cin_name = file;
  839.             else {
  840.                 cout_name = file;
  841.                 cout_append = (redir == 2);
  842.             }
  843.             inc = 0;
  844.         }
  845.  
  846.         if (inc) {                                   /* check elast for space */
  847.             ++ac;
  848.             CHECKAV( 1 );                            /* FUTURE: check retval */
  849.         }
  850.     } while( elast==HOT_BLANK );
  851.     av[ac] = NULL;
  852.  
  853.     /******************************************************************
  854.      * Part two:
  855.      * The argument line is processed (pipes, commands, recursive calls
  856.      */
  857.  
  858.     /* process pipes via files */
  859.  
  860.     if (elast == HOT_PIPE && !err) {
  861.         static int which;             /* 0 or 1 in case of multiple pipes */
  862.         which = 1 - which;
  863.         cout_name = tempname( which );
  864.         cout_ispipe = 1;
  865.     }
  866.  
  867.     if (err)
  868.         goto done0;
  869.  
  870.     {
  871.         char *avline=NULL;
  872.         char delim = ' ';
  873.         BPTR  oldcin  = Myprocess->pr_CIS, cin=NULL;
  874.         BPTR  oldcout = Myprocess->pr_COS, cout=NULL;
  875.         char *cin_buf=NULL;
  876.         struct FileHandle *ci=NULL;
  877.         long oldbuf=NULL;
  878.  
  879.         if( backg ) {
  880.             char *larg=av[ac-1];
  881.             memmove( av+1, av, ac*sizeof(*av));
  882.             command=av[0]=push_cpy( frameptr, (UBYTE*)o_rback);
  883.             ccno=find_command(command);
  884.             cstat=Command[ccno].stat;
  885.             if( Strlen(larg)>1 )
  886.                 larg[Strlen(larg)-1]=0, ac++;
  887.         }
  888.  
  889.         if( ccno==2 || (cstat & ST_A0))            /* alias with argument */
  890.             delim = 0xA0;
  891.  
  892.         if( cstat&ST_AVLINE || pend_alias || Verbose&VERBOSE_ALIAS )
  893.             avline=compile_avf(frameptr,av,(pend_alias?1:0), ac, delim,ccno==1);
  894.  
  895.         fflush(stdout);
  896.         LastCommand=command;
  897.         if ( !(cstat & (ST_NORED | ST_COND))) { /* redirection not disabled */
  898.             if (cin_name) {
  899.                 filemap(cin_name,0);
  900.                 if (!(cin = extOpen(cin_name,MODE_OLDFILE))) {
  901.                     ierror (NULL, 504);
  902.                     err = 20;
  903.                     cin_name = NULL;
  904.                 } else {
  905.                     redir_in=cin;
  906.                     if( !(cstat&ST_NORED)) {
  907.                         Myprocess->pr_CIS = DEVTAB(stdin) = cin;
  908.                         ci = (struct FileHandle *)(cin<<2);
  909.                         cin_buf = SAllocMem(202L, MEMF_PUBLIC);
  910.                         oldbuf = ci->fh_Buf;
  911.                         if (ci->fh_Buf == 0) /* fexec expects a CIS buffer */
  912.                             ci->fh_Buf = (long)cin_buf>>2;
  913.                     }
  914.                 }
  915.             }
  916.             if (cout_name) {
  917.                 filemap(cout_name,0);
  918.                 if (cout_append && (cout =extOpen(cout_name,1005L))) {
  919.                     Seek(cout, 0L, OFFSET_END );
  920.                 } else {
  921.                     cout = extOpen(cout_name,MODE_NEWFILE);
  922.                 }
  923.                 if (cout == NULL) {
  924.                     err = 20;
  925.                     ierror (NULL, 504);
  926.                     cout_name = NULL;
  927.                     cout_append = 0;
  928.                 } else {
  929.                     redir_out=cout;
  930.                     if( !(cstat&ST_STORED))
  931.                         Myprocess->pr_COS = DEVTAB(stdout) = cout;
  932.                 }
  933.             }
  934.         }
  935.  
  936.         if( Verbose&VERBOSE_ALIAS ) {
  937.             if( Verbose&VERBOSE_HILITE )
  938.                 fprintf(stderr,"%s",o_hilite), fflush(stderr);
  939.             if( pend_alias )
  940.                 fprintf(stderr,"%-*s%s %s\n",alias_count,">",av[0],avline);
  941.             else 
  942.                 fprintf(stderr,"%-*s%s\n",alias_count,">",avline);
  943.             if( Verbose&VERBOSE_HILITE )
  944.                 fprintf(stderr,"%s",o_lolite), fflush(stderr);
  945.         }
  946.  
  947.         if( pend_alias ) {
  948.             UBYTE *ptr, *scr;
  949.             FRAME *subframe=NULL;
  950.  
  951.             if( ccno==2 ) {                         /* has arguments */
  952.                 char *val=avline;
  953.                 int clen;
  954.  
  955.                 ptr=pend_alias;
  956.                 clen=Strlen(ptr)*2+20;
  957.                 newframe( &subframe, clen+DEFAULTFRAME );
  958.                 push_locals( (ROOT *)falloc( &subframe, sizeof(ROOT) ));
  959.                 do {                                       /* set all args    */
  960.                     char *varname, *gap=NULL;
  961.                     for( varname= ++ptr; *ptr && *ptr!=' ' && *ptr!='%'; ++ptr);
  962.                     if( *ptr=='%' && (gap=index(val,0xA0 )) ) *gap=0;
  963.                     set_var( LEVEL_LOCAL, varname, val );
  964.                     val= gap ? gap+1 : "";
  965.                 } while( *ptr=='%' );
  966.                 scr = falloc(&subframe,clen);
  967.             } else {
  968.                 int  clen=Strlen(pend_alias)+Strlen(avline)+20;
  969.  
  970.                 newframe( &subframe,3*clen+DEFAULTFRAME );
  971.                 push_locals( (ROOT *)falloc( &subframe, sizeof(ROOT) ));
  972.                 ptr = falloc( &subframe,clen );
  973.                 sprintf(ptr,"%s %s",pend_alias,avline);
  974.                 scr = falloc( &subframe, 2*clen );
  975.             }
  976.             preformat( ptr,scr );
  977.             err=fcomm (scr,&subframe,pend_alias );
  978.             pop_locals();
  979.             deleteframe(subframe);
  980.         } else {
  981.             if (ac < Command[ccno].minargs + 1) {
  982.                 show_usage( NULL );
  983.                 err = 20;
  984.             } else if (!err) {
  985.                 int (*func)(char*,int)=Command[ccno].func;
  986.                 get_opt( av, &ac, ccno, &err );
  987.                 if( err || ccno>0 && ac==2 && !strcmp(av[1],"?") )
  988.                     show_usage(av[0]);
  989.                 else 
  990.                     err = (*func)(avline, Command[ccno].val); /*  do the call */ 
  991.             }
  992.         }
  993.  
  994.         if (err < 0)
  995.             err = 20;
  996.         if (E_stack == 0 )
  997.             seterr(err);
  998.         fflush(stderr);
  999.  
  1000.         if (!(cstat & (ST_NORED | ST_COND))) {
  1001.             if (cin_name && !(cstat & ST_STORED)) {
  1002.                 ci->fh_Buf = oldbuf;
  1003.                 fflush(stdin);
  1004.                 clearerr(stdin);
  1005.                 RESETIO( stdin );
  1006.                 extClose(cin);
  1007.                 FreeMem(cin_buf, 202L);
  1008.             }
  1009.             if (cout_name && !(cstat & ST_STORED)) {
  1010.                 fflush(stdout);
  1011.                 clearerr(stdout);
  1012. #ifdef AZTEC_C
  1013.                 stdout->_flags &= ~_IODIRTY;    /* because of nil: device */
  1014. #endif
  1015.                 extClose(cout);
  1016.                 cout_append = 0;
  1017.             }
  1018.         }
  1019.         Myprocess->pr_CIS = DEVTAB(stdin)  = oldcin;
  1020.         Myprocess->pr_COS = DEVTAB(stdout) = oldcout;
  1021.  
  1022.         if (cin_ispipe && cin_name)
  1023.             DeleteFile(cin_name);
  1024.         if (cout_ispipe) {
  1025.             cin_name = cout_name;         /* ok to assign.. static name */
  1026.             cin_ispipe = 1;
  1027.         } else {
  1028.             cin_name = NULL;
  1029.         }
  1030.         cout_name = NULL;
  1031.         cout_ispipe = 0;
  1032.     }
  1033.  
  1034. done0:
  1035.     {
  1036.         char *exc;
  1037.         if (err && E_stack==0) {
  1038.             exc = get_var(LEVEL_SET, v_except);
  1039.             if (err >= ((exc)?atoi(exc):1)) {
  1040.                 if (exc) {
  1041.                     ++H_stack, ++E_stack;
  1042.                     a0tospace(exc);
  1043.                     exec_command(exc);
  1044.                     --E_stack, --H_stack;
  1045.                 } else {
  1046.                     Exec_abortline = 1;
  1047.                 }
  1048.             }
  1049.             seterr(err);
  1050.         }
  1051.         if (elast != 0 && Exec_abortline == 0) {
  1052.             memmove( str, nextstr, Strlen(nextstr)+1 );
  1053.             goto nextcommand;
  1054.         }
  1055.         Exec_abortline = 0;
  1056.         if (cin_name)
  1057.             DeleteFile(cin_name);
  1058.     }
  1059. done1:
  1060.  
  1061.     av=oldav; ac=oldac; max_ac=oldmax;
  1062.     --alias_count;
  1063.     return err;                      /* TRUE = error occured    */
  1064. }
  1065.  
  1066.  
  1067. static UBYTE *
  1068. exarg(UBYTE **ptr, UBYTE *elast)
  1069. {
  1070.     UBYTE *end, *start;
  1071.  
  1072.     start = end = *ptr;
  1073.     while ( *(signed char *)end>0 || *end && *end != 0x80 &&
  1074.              *end != HOT_SEMI &&  *end != HOT_PIPE && *end != HOT_BLANK)
  1075.         ++end;
  1076.     *elast = *end;
  1077.     *end = '\0';
  1078.     *ptr = end + 1;
  1079.     return start;
  1080. }
  1081.  
  1082.  
  1083. static void
  1084. newframe(FRAME **frameptr, int bytes)
  1085. {
  1086.     FRAME *new;
  1087.  
  1088.     new=salloc( bytes + 4 + sizeof(FRAME) );
  1089.     new->next= *frameptr;
  1090.     new->bytesleft = bytes;
  1091.     new->ptr       = new->mem;
  1092.     *frameptr=new;
  1093. }
  1094.  
  1095. static void *
  1096. falloc( FRAME **frameptr, int bytes )
  1097. {
  1098.     FRAME *frame= *frameptr;
  1099.     char *mem;
  1100.  
  1101.     bytes+=2;                                    /* 2 extra bytes for do_run  */
  1102.     bytes|=3;
  1103.     bytes++; 
  1104.  
  1105.     if( frame->bytesleft <= bytes )
  1106.         newframe( frameptr, bytes+DEFAULTFRAME ), frame=*frameptr;
  1107.  
  1108.     mem=frame->ptr;
  1109.     frame->bytesleft-=bytes;
  1110.     frame->ptr      +=bytes;
  1111.     return mem;
  1112. }
  1113.  
  1114. static void *
  1115. frealloc( FRAME **frameptr, char *oldstring, int morebytes )
  1116. {
  1117.     char *mem=oldstring;
  1118.  
  1119.     morebytes|=3;
  1120.     morebytes++; 
  1121.  
  1122.     if( (*frameptr)->bytesleft <= morebytes ) {
  1123.         int oldlen=(*frameptr)->ptr-oldstring;
  1124.         newframe( frameptr, oldlen+morebytes+DEFAULTFRAME );
  1125.         mem= (*frameptr)->ptr;
  1126.         strcpy((*frameptr)->ptr,oldstring);
  1127.         oldlen+=morebytes;
  1128.         (*frameptr)->ptr      +=oldlen;
  1129.         (*frameptr)->bytesleft-=oldlen;
  1130.     }
  1131.     (*frameptr)->bytesleft-=morebytes;
  1132.     (*frameptr)->ptr      +=morebytes;
  1133.     return mem;
  1134. }
  1135.  
  1136. static void
  1137. funalloc( FRAME **frameptr, int bytes )
  1138. {
  1139.     bytes&=~3;
  1140.     (*frameptr)->bytesleft+=bytes;
  1141.     (*frameptr)->ptr      -=bytes;
  1142. }
  1143.  
  1144.  
  1145.  
  1146. static void
  1147. deleteframe(FRAME *frame)
  1148. {
  1149.     FRAME *nxt;
  1150.  
  1151.     for( ; frame; frame=nxt ) {
  1152.         nxt=frame->next;
  1153.         free(frame);
  1154.     }
  1155. }
  1156.  
  1157. /*
  1158.  * Insert 'from' string in front of 'str' while deleting the
  1159.  * first entry in 'str'.  if freeok is set, then 'str' will be
  1160.  * free'd
  1161.  */
  1162.  
  1163. static UBYTE *
  1164. format_insert_string(FRAME **frameptr, UBYTE *str, char *from)
  1165. {
  1166.     UBYTE *new, *strskip;
  1167.     int len;
  1168.  
  1169.     strskip=skipword( str );
  1170.     len = Strlen(from)*3+20;
  1171.     new = falloc( frameptr, len);
  1172.     preformat(from, new);
  1173.     funalloc( frameptr, len -Strlen(new)  - 2 );
  1174.     frealloc( frameptr, (char*)new, Strlen(strskip)+2);
  1175.     strcat((char*)new, (char*)strskip);
  1176.     new[Strlen(new)+1] = 0;
  1177.     return new;
  1178. }
  1179.  
  1180. static UBYTE *
  1181. skipword( UBYTE *strskip )
  1182. {
  1183.     for ( ; *(signed char *)strskip>0
  1184.         || *strskip && *strskip != HOT_BLANK 
  1185.         && *strskip != HOT_SEMI && *strskip != HOT_PIPE
  1186.         && *strskip != 0x80; ++strskip);
  1187.     return strskip;
  1188. }
  1189.  
  1190. char *
  1191. find_internal( char *str )
  1192. {
  1193.     return Command[find_command(str)].name;
  1194. }
  1195.  
  1196. static int
  1197. find_command( char *str )
  1198. {
  1199.     int i, len = Strlen(str);
  1200.     struct COMMAND *com;
  1201.     char c=*str;
  1202.  
  1203.     if( o_abbrev!=2 )
  1204.         for( com=Command, i=0; com->func; com++, i++ )
  1205.             if ( c==*com->name && !strncmp(str, com->name, len))
  1206.                 if(o_abbrev || len==Strlen(com->name))
  1207.                     return i;
  1208.     if( !Strcmp(BaseName(str),"Execute") || !Strcmp(BaseName(str),"Run"))
  1209.         return COM_EXEC;
  1210.  
  1211.     return 0;
  1212. }
  1213.  
  1214. int exec_fn_err;
  1215.  
  1216. extern struct FUNCTION {
  1217.     short id, minargs, maxargs;
  1218.     char *name;
  1219. } Function[];
  1220.  
  1221.  
  1222. char *gotfunc( int i, char **fav, int fac );
  1223.  
  1224. char *
  1225. exec_function( char *str, char **fav, int fac)
  1226. {
  1227.     int len=Strlen(str)-1, i;
  1228.  
  1229.     exec_fn_err=0;
  1230.     for (i = 0; Command[i].func; ++i)
  1231.         if ( Command[i].stat&ST_FUNC && !strncmp(str,Command[i].name,len)) {
  1232.             if( fac<Command[i].minargs ) {
  1233.                 exec_fn_err=20;
  1234.                 return NULL;
  1235.             } else {
  1236.                 int (*func)( void )=Command[i].func;
  1237.                 char **oldav=av;
  1238.                 int  oldac=ac;
  1239.                 av=fav-1, ac=fac+1;
  1240.                 exec_fn_err=(*func)();
  1241.                 av=oldav, ac=oldac;
  1242.                 return get_var( LEVEL_SET, fav[0] );
  1243.             }
  1244.         }
  1245.     for (i = 0; Function[i].id; ++i)
  1246.         if ( len==Strlen(Function[i].name)&&!strncmp(str,Function[i].name,len))
  1247.             return gotfunc( i,fav,fac );
  1248.  
  1249.     exec_fn_err=-1;
  1250.     return NULL;
  1251. }
  1252.  
  1253. int
  1254. echofunc(void)
  1255. {
  1256.     int  i;
  1257.     char *str;
  1258.  
  1259.     if( !strlen(av[0]) ) return -1;
  1260.     exec_fn_err=0;
  1261.     for (i = 0; Function[i].id; ++i)
  1262.         if ( !strcmp(av[0],Function[i].name)) {
  1263.             if(str=gotfunc( i,av,ac ))
  1264.                 printf("%s\n",str);
  1265.             return exec_fn_err;
  1266.         }
  1267.     return -1;
  1268. }
  1269.  
  1270.  
  1271. char *
  1272. gotfunc( int i, char **fav, int fac )
  1273. {
  1274.     fac--; fav++;
  1275.     if( fac<Function[i].minargs ) {
  1276.         fprintf( stderr, "Not enough arguments for @%s\n",
  1277.                           Function[i].name );
  1278.         exec_fn_err=20;
  1279.         return NULL;
  1280.     } else if( fac>Function[i].maxargs ) {
  1281.         if( ac > Function[i].maxargs )
  1282.             fprintf( stderr, "Too many arguments for @%s\n",
  1283.                               Function[i].name );
  1284.         exec_fn_err=20;
  1285.         return NULL;
  1286.     } else {
  1287.         exec_fn_err=dofunc( Function[i].id, fav, fac);
  1288.         return exec_fn_err ? (char *)NULL : (char *)get_var(LEVEL_SET, v_value);
  1289.     }
  1290.     return NULL;
  1291. }
  1292.  
  1293.  
  1294.  
  1295. do_help()
  1296. {
  1297.     struct COMMAND *com;
  1298.     int i=0;
  1299.  
  1300.     for (com = &Command[FIRSTCOMMAND]; com->func; ++com) {
  1301.         printf ("%-12s", com->name);
  1302.         if (++i % 6 == 0) printf("\n");
  1303.     }
  1304.     printf("\n\nUse   man <command>   for more information\n");
  1305.     return 0;
  1306. }
  1307.  
  1308. do_nothing()
  1309. {
  1310.     return 0;
  1311. }
  1312.  
  1313. static char *
  1314. push_cpy(FRAME **frameptr, void *s)
  1315. {
  1316.     return strcpy(falloc(frameptr,Strlen(s)+1), s);
  1317. }
  1318.  
  1319. void
  1320. exec_every(void)
  1321. {
  1322.     char *str = o_every;
  1323.  
  1324.     if (str) {
  1325.         ++H_stack, ++E_stack;
  1326.         a0tospace( str );
  1327.         exec_command(str);
  1328.         --E_stack, --H_stack;
  1329.     }
  1330. }
  1331.  
  1332. char *
  1333. a0tospace( char *str )
  1334. {
  1335.     char *get=str, *put=str;
  1336.  
  1337.     while( *get )
  1338.         if( (UBYTE)*get==0xA0 )
  1339.             *put++=' ', get++;
  1340.         else 
  1341.             put++,get++;
  1342.     return str;
  1343. }
  1344.  
  1345. void
  1346. show_usage( char *str )
  1347. {
  1348.     int ccno, first=0, err=0;
  1349.     char *get, *put, buf[300];
  1350.  
  1351.     if( !str )
  1352.         str=LastCommand, err=1;
  1353.     for( put=str; *put && (*put&127)!=32; put++ ) ;
  1354.     *put=0;
  1355.  
  1356.     put=buf;
  1357.     ccno = find_command (str);
  1358.     if( get= Command[ccno].usage ) {
  1359.         do {
  1360.             put+=sprintf(put, first++?"       %s ":"Usage: %s ",
  1361.                          Command[ccno].name );
  1362.             if( *get=='-' ) {
  1363.                 *put++='['; *put++='-';
  1364.                 get++;
  1365.                 while( *get && *get!=' ' && *get!=',' )
  1366.                     *put++=*get++;
  1367.                 *put++=']';
  1368.             }
  1369.             while( *get && *get!=','  )
  1370.                 *put++=*get++;
  1371.             *put++='\n';
  1372.         } while( *get++ );
  1373.         *put=0;
  1374.         fprintf( err ? stderr : stdout, "%s", buf );
  1375.     }
  1376. }
  1377.  
  1378. do_exec( char *str )
  1379. {
  1380.     if( !options ) 
  1381.         return execute( next_word( str ) );
  1382.     execute( next_word(next_word( str )));
  1383.     return 0;
  1384. }
  1385.  
  1386. static int
  1387. checkav( FRAME **frameptr, int n )
  1388. {
  1389.     char **tmp;
  1390.     int newac;
  1391.  
  1392.     if( ac+n+10>=max_ac ) {
  1393.         newac=max_ac+n+40;
  1394.         tmp=(char **)falloc(frameptr,newac*sizeof(char *));
  1395.         memcpy(tmp,av,max_ac*sizeof(char *));
  1396.         av=tmp; max_ac=newac;
  1397.     }
  1398.     return 0;
  1399. }
  1400.  
  1401. /*    Parse the options specified in sw[]
  1402.     Setting a bit in global variable options
  1403.     for each one found
  1404. */
  1405.  
  1406. int
  1407. get_opt( char **av, int *ac, int ccno, int *err )
  1408. {
  1409.     static char opts[2];
  1410.     char **get=av+1,**put=av+1, *c, *s, *str;
  1411.     int  i=1, l, usage=0, nac;
  1412.     long oldopts, origopts=options;
  1413.  
  1414.     *err=0;
  1415.     options=0;
  1416.     if( !ccno )
  1417.         return 0;
  1418.     if( ccno>0 )
  1419.         str=Command[ccno].options;
  1420.     else 
  1421.         str=opts, opts[0]=-ccno;
  1422.  
  1423.     for( ; i<*ac && *av[i]=='-'; i++, get++ ) {
  1424.         if( !*(c=*get+1))
  1425.             goto stop;
  1426.         if(  *c=='-' && !c[1] ) {
  1427.             i++, get++;
  1428.             goto stop;
  1429.         }
  1430.         oldopts=options;
  1431.         for ( ; *c ; c++) {
  1432.             if( *c<'a' || *c>'z' ) 
  1433.                 { options=oldopts; goto stop; }
  1434.             for( l=0, s=str; *s && *s != *c; ++s )
  1435.                 ++l;
  1436.             if ( *s )
  1437.                 options |= (1 << l);
  1438.             else if( !usage ) {
  1439.                 usage=1;
  1440.                 if( ccno>0 )
  1441.                     *err=20;
  1442.             }
  1443.         }
  1444.     }
  1445. stop:
  1446.     if( ccno>0 ) {
  1447.         for( nac=1; i<*ac; i++ )
  1448.             *put++=*get++, nac++;
  1449.         *put=NULL;
  1450.         *ac=nac;
  1451.     } else {
  1452.         i=options;
  1453.         options=origopts;
  1454.         return i;
  1455.     }
  1456.     return 0;
  1457. }
  1458.  
  1459. #if 0
  1460. USHORT Options[160];
  1461.  
  1462. int
  1463. do_options()
  1464. {
  1465.     for( i=1; i<ac; i+=2 ) {
  1466.         if( ac-i<=1 )
  1467.             { ierror( av[i], 500 ); return 20; }
  1468.         if( *av[i+1]!='-' )
  1469.             { ierror( av[i+1], 500 ); return 20; }
  1470.         options=0;
  1471.         parseopts( av[i+1]+1 );
  1472.     }
  1473. }
  1474. #endif
  1475.  
  1476. extern char *MyMem;
  1477. static char Pipe[3][32];
  1478.  
  1479. static char *
  1480. tempname( int which )
  1481. {
  1482.     sprintf(Pipe[which],"%spipe%c%d_%lx",o_pipe,'A'+which,alias_count,MyMem);
  1483.     return Pipe[which];
  1484. }
  1485.  
  1486. int
  1487. hasspace( char *s )
  1488. {
  1489.     if( !*s )
  1490.         return 1;
  1491.     for ( ; *s; s++)
  1492.         if (ISSPACE(*s) || *s==';') return 1;
  1493.     return 0;
  1494. }
  1495.  
  1496. static char *
  1497. compile_avf(FRAME **framep,char **av, int start, int end, char delim, int quote)
  1498. {
  1499.     char *cstr, *p, *s;
  1500.     int len, i;
  1501.  
  1502.     len = 3;
  1503.     for (i = start; i < end; ++i) len += Strlen(av[i]) + 3;
  1504.     if( framep ) {
  1505.         p = cstr = falloc(framep,len);
  1506.     } else 
  1507.         p = cstr = salloc(len);
  1508.     *cstr = '\0';
  1509.  
  1510.     for (i = start; i < end; ++i) {
  1511.         if (quote && hasspace(av[i]))
  1512.             p += sprintf(p, "\"%s\"", av[i]);
  1513.         else {
  1514.             for( s=av[i]; *p++=*s++; ) ;
  1515.             p--;
  1516.         }
  1517.         if (i+1 < end) *p++=delim;
  1518.     }
  1519.     *p='\0';
  1520.  
  1521.     return cstr;
  1522. }
  1523.  
  1524. char *
  1525. compile_av(char **av, int start, int end, char delim, int quote)
  1526. {
  1527.     return compile_avf(NULL,av, start, end, delim, quote);
  1528. }
  1529.  
  1530. int
  1531. myfgets( char *buf, FILE *in )
  1532. {
  1533.     int l;
  1534.     char *ret;
  1535.     if( ret=fgets(buf,253,in) ) {
  1536.         l=Strlen(buf);
  1537.         if( buf[l-1]=='\n' )
  1538.             buf[l-1]=0;
  1539.     }
  1540.     return ret!=NULL && !dobreak();
  1541. }
  1542.